package br.usp.icmc.client.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import br.usp.icmc.client.entidades.KeyWord;
import br.usp.icmc.client.entidades.Service;


/*******************************************************************
 * @author felipe
 *
 * <b> <br> Projeto: Aplicativo Cliente <br><b>
 *
 * Classe de acesso a tabela Keyword
 * 
 *******************************************************************/
public class KeyWordDAO extends HibernateAbstractDAO<KeyWord, Long>{

	@Override
	public List<KeyWord> select(String key) {
		// TODO Auto-generated method stub
		return null;
	}
	
	/*******************************************************************
	 * Busca todas as palavra-chave do sistema
	 * 
	 * @return
	 * 		List com todas as palavras do sistema, ordenadas por nome.
	 *******************************************************************/
	public List selectAllWord(){
		List kw = getSession().createCriteria(KeyWord.class)
				.addOrder(Order.asc("word"))
				.list();	
		return kw;
	}
	
	/**********************************************************************
	 * Busca todas as palavras-chave com tal id
	 * 
	 * @param id
	 * 			ID da palavra
	 * @return
	 * 		List com a palavra-chave desejada
	 ***********************************************************************/
	public List selectByID(Long id){
		List kw = getSession().createQuery("from KeyWord kw where kw.idKeyWord='"+id+"'").list();
		return kw;
	}


	/**
	 * Busca as keywords que são pais
	 * 
	 * @return
	 */
	public List selectParent(){
		final String hql = "select distinct(w) from KeyWord as w inner join w.words as parent";  
		  
		return getSession().createQuery(hql)     
		                              .list();
	}
	
	/**
	 * Lista as keywords filhas de determinado pai
	 * 
	 * @param idKey
	 * 			ID da keyword pai
	 * @return
	 * 			Lista de keyword filhas
	 */
	public List selectLeafByParent(Long idKey){
		final String hql = "select distinct(parent) from KeyWord as w inner join w.words as parent where w.idKeyWord in (:word)";  
		  
		return getSession().createQuery(hql)     
				.setParameter("word", idKey)  
                .list();
	}
	
	/**
	 * Lista todas as keywords
	 * 
	 * @return
	 */
	public List selectWords(){
		List kw = getSession().createCriteria(KeyWord.class)
				.addOrder(Order.asc("word"))
				.list();	
		return kw;
	}
	
	
}
